package com.guigu.qingcheng.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.guigu.qingcheng.mapper.TbOrderMapper;
import com.guigu.qingcheng.mapper.TbReturnOrderItemMapper;
import com.guigu.qingcheng.mapper.TbReturnOrderMapper;
import com.guigu.qingcheng.pojo.TbOrder;
import com.guigu.qingcheng.pojo.TbReturnOrder;
import com.guigu.qingcheng.pojo.TbReturnOrderItem;
import com.guigu.qingcheng.service.TbReturnOrderService;
import com.guigu.qingcheng.utils.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
*
* @author Jiang
* @date 2020/07/31
*/
@Service
public class TbReturnOrderServiceImpl implements TbReturnOrderService {

    @Autowired(required = false)
    private TbReturnOrderMapper tbReturnOrderMapper;

    @Autowired(required = false)
    private TbReturnOrderItemMapper tbReturnOrderItemMapper;

    @Autowired(required = false)
    private TbOrderMapper tbOrderMapper;


    @Override
    public Map findBystatus(Integer status) {
        return tbReturnOrderMapper.findreturnorder(status);
    }

    @Override
    public List<TbReturnOrder> queryAll() {
        List<TbReturnOrder> tbReturnOrder = tbReturnOrderMapper.selectAll();
            return tbReturnOrder;
    }

    @Override
    public PageUtil<TbReturnOrder> findListAll(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        Page<TbReturnOrder> pages = (Page<TbReturnOrder>) tbReturnOrderMapper.selectAll();
        PageUtil<TbReturnOrder> pageUtil=new PageUtil<>();
        pageUtil.setRows(pages.getResult());
        pageUtil.setTotal(pages.getTotal());
        return pageUtil;
    }


    @Override
    public PageUtil<TbReturnOrder> pageQueryByAll(Integer pageNum, Integer pageSize, TbReturnOrder tbReturnOrder) {

        Example example=new Example(TbReturnOrder.class);
        Example.Criteria criteria = example.createCriteria();
        // TODO 待实现 重新实现判断参数

        if(!StringUtils.isEmpty(tbReturnOrder.getId())){
            criteria.andEqualTo("id",tbReturnOrder.getId());
        }
        if(!StringUtils.isEmpty(tbReturnOrder.getLinkman())){
           criteria.andLike("linkman","%"+tbReturnOrder.getLinkman()+"%");
        }
        if(!StringUtils.isEmpty(tbReturnOrder.getApplyTime())){
            criteria.andEqualTo("applyTime",tbReturnOrder.getApplyTime());
        }
        if(!StringUtils.isEmpty(tbReturnOrder.getType())){
            criteria.andEqualTo("type",tbReturnOrder.getType());
        }
        PageHelper.startPage(pageNum,pageSize);
        Page<TbReturnOrder> pages = (Page<TbReturnOrder>) tbReturnOrderMapper.selectByExample(example);
        PageUtil<TbReturnOrder> pageUtil=new PageUtil<>();
        pageUtil.setRows(pages.getResult());
        pageUtil.setTotal(pages.getTotal());
        return pageUtil;
     }


    @Override
    public boolean saveTbReturnOrder(TbReturnOrder tbReturnOrder) {
        int insert = tbReturnOrderMapper.insert(tbReturnOrder);
        if(insert>0){
            return true;
        }
        return false;
    }

    @Override
    public boolean updateTbReturnOrder(TbReturnOrder tbReturnOrder) {
        tbReturnOrder.setDisposeTime(new Date());
        int i = tbReturnOrderMapper.updateByPrimaryKeySelective(tbReturnOrder);
        if(i>0){
            return true;
        }
        return false;
    }


    @Override
    public boolean deleteById(Integer id) {
        int i = tbReturnOrderMapper.deleteByPrimaryKey(id);
        if(i>0){
            return true;
        }
        return false;
    }

    @Override
    public TbReturnOrder findById(Long id) {
        TbReturnOrder tbReturnOrder = tbReturnOrderMapper.selectByPrimaryKey(id);
        return tbReturnOrder;
    }

    @Override
    public boolean batchDelete(String ids) {
        String[] id = ids.split(",");
        for (int i=0;i<id.length;i++){
            tbReturnOrderMapper.deleteByPrimaryKey(id[i]);
        }
        return true;
    }

    @Override
    public TbOrder queryByReturnId(Long id) {
        Example example=new Example(TbReturnOrderItem.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("returnOrderId",id);
        List<TbReturnOrderItem> list = tbReturnOrderItemMapper.selectByExample(example);
        if(list.size()>0){
            TbReturnOrderItem tbReturnOrderItem = list.get(0);
            TbOrder order = tbOrderMapper.selectByPrimaryKey(tbReturnOrderItem.getOrderId());
            return order;
        }
        return null;
    }
}
